package com.zj.viewpagerdemo.view;

import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.zj.viewpagerdemo.R;
import com.zj.viewpagerdemo.adapter.MainAdapter;
import com.zj.viewpagerdemo.base.BaseActivity;
import com.zj.viewpagerdemo.mvp.MainContract;
import com.zj.viewpagerdemo.mvp.MainPresenter;

public class MainActivity extends BaseActivity implements MainContract.View, View.OnClickListener {
    private static final String TAG = MainActivity.class.getSimpleName();

    private ViewPager viewPager;
    private Button btnFirst;
    private Button btnLeft;
    private Button btnRight;
    private Button btnLast;

    private MainContract.Presenter presenter;
    private MainAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        new MainPresenter(this);
    }

    @Override
    public void setPresenter(MainContract.Presenter presenter) {
        this.presenter = presenter;
    }

    @Override
    public void initViews() {
        viewPager = (ViewPager) findViewById(R.id.view_pager);
        btnFirst = (Button) findViewById(R.id.btn_first);
        btnLeft = (Button) findViewById(R.id.btn_left);
        btnRight = (Button) findViewById(R.id.btn_right);
        btnLast = (Button) findViewById(R.id.btn_last);

        adapter = new MainAdapter(getSupportFragmentManager());
        viewPager.setAdapter(adapter);

        btnFirst.setOnClickListener(this);
        btnLeft.setOnClickListener(this);
        btnRight.setOnClickListener(this);
        btnLast.setOnClickListener(this);
    }

    @Override
    public void onLoading(String text) {
        showLoading(text);
    }

    @Override
    public void loadingComplete() {
        hideLoading();
    }

    @Override
    public void loadingError(String error) {

    }

    @Override
    public void onClick(View v) {
        int index = viewPager.getCurrentItem();
        int maxNUM = 5;
        Log.d(TAG, "onClick: index = " + index + " maxNUM = " + maxNUM);

        switch (v.getId()) {
            case R.id.btn_first:
                viewPager.setCurrentItem(0);
                break;
            case R.id.btn_left:
                viewPager.setCurrentItem(index - 1);
                break;
            case R.id.btn_right:
                viewPager.setCurrentItem(index + 1);
                break;
            case R.id.btn_last:
                viewPager.setCurrentItem(maxNUM - 1);
                break;
        }
    }
}
